package com.ruoyi.factorydata.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.factorydata.domain.MaterialRequisitionDetail;
import com.ruoyi.factorydata.service.IMaterialRequisitionDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 领料单详情Controller
 *
 * @author ruoyi
 * @date 2025-07-29
 */
@RestController
@RequestMapping("/materialRequisition/materialRequisitionDetail")
public class MaterialRequisitionDetailController extends BaseController {
    @Autowired
    private IMaterialRequisitionDetailService materialRequisitionDetailService;

    /**
     * 查询领料单详情列表
     */
    @PreAuthorize("@ss.hasPermi('materialRequisition:materialRequisitionDetail:list')")
    @GetMapping("/list")
    public TableDataInfo list(MaterialRequisitionDetail materialRequisitionDetail) {
        startPage();
        List<MaterialRequisitionDetail> list = materialRequisitionDetailService.selectMaterialRequisitionDetailList(materialRequisitionDetail);
        return getDataTable(list);
    }

    /**
     * 导出领料单详情列表
     */
    @PreAuthorize("@ss.hasPermi('materialRequisition:materialRequisitionDetail:export')")
    @Log(title = "领料单详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MaterialRequisitionDetail materialRequisitionDetail) {
        List<MaterialRequisitionDetail> list = materialRequisitionDetailService.selectMaterialRequisitionDetailList(materialRequisitionDetail);
        ExcelUtil<MaterialRequisitionDetail> util = new ExcelUtil<MaterialRequisitionDetail>(MaterialRequisitionDetail.class);
        util.exportExcel(response, list, "领料单详情数据");
    }

    /**
     * 获取领料单详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('materialRequisition:materialRequisitionDetail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(materialRequisitionDetailService.selectMaterialRequisitionDetailById(id));
    }

    /**
     * 获取领料单详情列表根据工单id
     */
    @GetMapping("/getListByWorkOrderId/{workOrderId}")
    public AjaxResult getListByWorkOrderId(@PathVariable("workOrderId") Long workOrderId) {
        return success(materialRequisitionDetailService.getListByWorkOrderId(workOrderId));
    }


    @GetMapping("/scanner/{codeSlot}")
    public AjaxResult scanner(@PathVariable("codeSlot") String codeSlot) {
        return success(materialRequisitionDetailService.scanner(codeSlot));
    }

    /**
     * 新增领料单详情
     */
    @PreAuthorize("@ss.hasPermi('materialRequisition:materialRequisitionDetail:add')")
    @Log(title = "领料单详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MaterialRequisitionDetail materialRequisitionDetail) {
        return toAjax(materialRequisitionDetailService.insertMaterialRequisitionDetail(materialRequisitionDetail));
    }

    /**
     * 修改领料单详情
     */
    @PreAuthorize("@ss.hasPermi('materialRequisition:materialRequisitionDetail:edit')")
    @Log(title = "领料单详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MaterialRequisitionDetail materialRequisitionDetail) {
        return toAjax(materialRequisitionDetailService.updateMaterialRequisitionDetail(materialRequisitionDetail));
    }

    /**
     * 删除领料单详情
     */
    @PreAuthorize("@ss.hasPermi('materialRequisition:materialRequisitionDetail:remove')")
    @Log(title = "领料单详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(materialRequisitionDetailService.deleteMaterialRequisitionDetailByIds(ids));
    }
}
